package com.jdz.tools;

import com.jdz.dao.FlyDao;
import com.jdz.dao.NoiseDao;
import com.jdz.entities.AirGps;
import com.jdz.entities.FlyRow;



import javax.annotation.Resource;

public class Interpolation {

    @Resource
    private FlyDao flyDao;

    @Resource
    private NoiseDao noiseDao;


    public static final double PI = 3.14159265358979324;
    // 赤道半径(单位m)
    private static final  double EARTH_RADIUS = 6378137;

    /**
     * 转化为弧度(rad)
     * */
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }




    //计算声音传播的时间
    public static double distance(double[] gps1,double[] gps2,double a_speed)//将飞行参数插值到噪声中
    {   //gps1,飞机坐标
        //gps2,声道坐标
        //经度，经分，经秒，纬度，纬分，纬秒，高度（秒）
        double time;
        double d;
        double lon1= gps1[0]+gps1[1]/60+gps1[2]/3600;
        double lat1= gps1[3]+gps1[4]/60+gps1[5]/3600;
        double h1 = gps1[6];

        double lon2= gps2[0]+gps2[1]/60+gps2[2]/3600;
        double lat2= gps2[3]+gps2[4]/60+gps2[5]/3600;
        double h2 = gps2[6];


        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lon1) - rad(lon2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2)+Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s * EARTH_RADIUS;
        //s = Math.round(s * 10000) / 10000;
        d = Math.sqrt(Math.pow(s,2)+Math.pow(h1-h2,2));

        return d/a_speed;
    }






    public static double dis_time(double[] gps1,double[] gps2)//将飞行参数插值到噪声中
    {   //gps1,飞机坐标
        //gps2,声道坐标
        //经度，经分，经秒，纬度，纬分，纬秒，高度（秒）
        double time;
        double d;
        double lon1= gps1[0]+gps1[1]/60+gps1[2]/3600;
        double lat1= gps1[3]+gps1[4]/60+gps1[5]/3600;
        double h1 = gps1[6];

        double lon2= gps2[0]+gps2[1]/60+gps2[2]/3600;
        double lat2= gps2[3]+gps2[4]/60+gps2[5]/3600;
        double h2 = gps2[6];


        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lon1) - rad(lon2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2)+Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
        s = s * EARTH_RADIUS;
        //s = Math.round(s * 10000) / 10000;
        d = Math.sqrt(Math.pow(s,2)+Math.pow(h1-h2,2));

        return d/340;
    }


    //测试FlyRow

    public static FlyRow getOne()
    {
        double[] data = new double[14];
        for(int i = 0;i<=13;i++)
        {
            data[i] = i*20;
        }
        FlyRow flyRow = new FlyRow(data);

        System.out.println(flyRow.toString());
        return flyRow;
    }







    //拿到时间差，就可以对飞行参数进行操作了
    //首先，从第一个开始查询，然后for gps数组，针对每一个数组都计算时间差，计算到时间差后，然后根据数据的差值
    //根据这个数据差，然后用当前时间减去时间原点的值加上时间差，再加上飞参数据的时间阈值（如1/3秒）
    // 每次飞参都把从加上时间差到该加完的1/3秒钟所有的噪声数据的尾部填满飞参
    // 再把该条数据插入到新的表中即可



    //遍历添加，时间同步
    public int sync(double[] gps_ground)//声道gps
    {
        int x = 0;
        //遍历ArrayList<FlyRow>;
        for(FlyRow f:flyDao.getAllFly())
        {
            x = addFly(f,gps_ground);
        }
        return x;
    }
    //添加
    public int addFly(FlyRow f,double[] gps_ground)
    {
        int fileid = f.getFile_id();
        double freq = flyDao.getFreq(fileid);//此处时间复杂度后期可进行优化
        double T = 1/freq;//飞参测试周期

        AirGps airGps = flyDao.getFlyGps(f.getId());//查出飞机gps
        double[] gps1 = airGps.toArray();
        //计算出偏移后的时间
        double begin = f.getTime()+dis_time(gps1,gps_ground);

        //开始添加
        return noiseDao.addFlyStatus(f,begin,begin+T);

    }

    public static void main(String[] args) {

    }


}
